﻿Type=Activity
Version=6
ModulesStructureVersion=1
B4A=true
@EndOfDesignText@
#Region  Activity Attributes 
	#FullScreen: False
	#IncludeTitle: True
#End Region

Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.

End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.
	Private ulv As UltimateListView
	Private pyhq As Panel
	Dim bd As BetterDialogs
	Private lstData As List
	Private bLoading=False,bScrolling=False As Boolean '下拉刷新用的
	Private edtyhqno As EditText
	Private edtyhqpass As EditText
	Private isSel As Boolean=False
	Private cb As Object
	Private btnadd As Label
	Private btnleft As Label
	Private Panel1 As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
Dim telaw As telanywhere
telaw.Initialize(Activity)

	'Do not forget to load the layout file created with the visual designer. For example:
	Activity.LoadLayout("layyhqlist")
	initviews
	
End Sub
Private Sub initviews
	ulv.Initialize(comm.N_ULVMEMCACHE,comm.N_ULVDISKCACHE,comm.S_ULVCACHEDIR,"ulv")
	ulv.Color=0xffeeeeee
	comm.setFA(btnadd,"fa-plus","")
	comm.setFA(btnleft,"fa-angle-left","")
	pyhq.AddView(ulv,0,0,pyhq.Width,pyhq.Height)
	Dim nh As Int=150dip
	ulv.AddLayout("itemyhq","ulv_LayoutCreator","ulv_ContentFiller",nh,True)
	ulv.AddLayout("itemrefush","ulv_LayoutCreator","ulv_ContentFiller",60dip,True)
	lstData.Initialize
	If g_store.conf.ContainsKey("yhqlst") Then
		lstData=g_store.conf.Get("yhqlst")
		fillulv
	End If
	reqYhq
End Sub
Sub selyhq(tcb As Object)
	isSel=True
	cb=tcb
End Sub
Private Sub reqYhq
	bLoading=True
	ProgressDialogShow("正在获取优惠券")
	ParseHelper.doGetReq(Me,"yhq",comm.getApi(comm.apiIdx.GETYHQ),Null)
	'ParseHelper.doGetReq(Me,"yhq","http://192.168.1.3:8080/t.php",Null)
End Sub
'下拉刷新

Sub ulv_Overscrolled(Action As Int, ScrollY As Int)
	'Log("A:"&Action&"  Y:"&ScrollY)
	Select Action
	Case 2'按下
		If bScrolling=False And ScrollY<-100 Then 
			ulv.InsertItemAt("itemrefush",0,0)	
			bScrolling=True
		End If
	Case 1'放开
		If bScrolling Then
			bScrolling=False
			ulv.RemoveItemAt(0)
			If ScrollY<-100 And bLoading=False Then '防止重复加载
				Log("req refush once")
				reqYhq
			End If
		End If
	End Select
End Sub

Private Sub fillulv
	ulv.ClearContent
	For i=0 To lstData.Size-1
		ulv.AddItem("itemyhq",i)
	Next
	ulv.RefreshContent
End Sub
Sub ulv_EmptyView(LayoutPanel As Panel)
	LayoutPanel.LoadLayout("itemempty")
End Sub
Sub JobDone(thj As HttpJob)
	ProgressDialogHide
	bLoading=False
	If thj.Success Then
		Dim str As String=thj.GetString
		Log(str)
		Dim nr As netRes=ParseHelper.getStatusData(str)
		If nr.nCode=200 Then
			Select thj.JobName
			Case "yhq"
				lstData.Clear
				If ParseHelper.parseYhq(str) Then
					lstData=g_store.conf.Get("yhqlst")
					fillulv
				Else
					Msgbox(nr.sData,"错误")
				End If
			Case "addyhq"
				comm.T("添加成功")
			End Select
		Else
			'comm.TL(thj.JobName&"错误:"&nr.sData)
			comm.TL("错误:"&nr.sData)
		End If
	Else
		comm.TL(thj.JobName&"网络错误:"&thj.ErrorMessage)
	End If
End Sub
Sub ulv_ContentFiller(ItemID As Long, LayoutName As String, LayoutPanel As Panel, Position As Int)
	'LayoutName=LayoutName.ToLowerCase '这里传进来的是大写必须转换一下
	'Log(LayoutName)
	Select LayoutName.ToLowerCase
	Case "itemyhq"
		Dim p As Panel=LayoutPanel.GetView(0)
		Dim lbluselimit As Label=p.GetView(1)
		Dim lbltimerange As Label=p.GetView(2)
		Dim lblyhqje As Label=p.GetView(3)
		Dim yhq As yhq=lstData.Get(ItemID)
		If yhq.withLimit Then
			lbluselimit.Text=$"元优惠券（满$1.0{yhq.uselimit}元使用）"$
		Else
			lbluselimit.Text="元优惠券（无使用限制）"
		End If
		lbltimerange.Text="有效期："&yhq.starttime&" 至 "&yhq.endtime
		lblyhqje.Text=$"$1.0{yhq.money}"$	
	End Select
	
End Sub
Sub ulv_ItemClick(ItemID As Long, Position As Int, ClickedPanel As Panel)
	ToastMessageShow("ID:"&ItemID&" clicked",False)
	If isSel Then
		If isyhqExpire(lstData.Get(ItemID)) Then
			Msgbox("当前选择的优惠券已过期","已过期")
		Else
			CallSubDelayed2(cb,"yhqselected",lstData.Get(ItemID))
			isSel=False		
			Activity.Finish
		End If
		
	End If
	
End Sub
'判断优惠券过期了没,true=过期了
Private Sub isyhqExpire(yhq As yhq) As Boolean
	Dim dtnow As Long=DateTime.Now
	DateTime.DateFormat="yyyy-MM-dd"
	Dim dtto As Long=DateTime.DateParse(yhq.endtime&" 00:00:01")
	If dtnow>dtto Then
		Return True
	Else
		Return False
	End If
End Sub
Sub ulv_LayoutCreator(LayoutName As String, LayoutPanel As Panel)
	LayoutPanel.LoadLayout(LayoutName)

End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub cd1_BackKeyPressed As Boolean
	bd.CloseDialog(DialogResponse.CANCEL)
	Return True
End Sub



Sub btndlgok_Click
	bd.CloseDialog(DialogResponse.POSITIVE)
	If edtyhqno.Text.Trim.Length>0 And edtyhqpass.Text.Trim.Length>0 Then
		comm.showProgress("正在提交")
		ParseHelper.doPostReqArr(Me,"addyhq",comm.getApi(comm.apiIdx.ADDYHQ),Array As String("quanhao",edtyhqno.Text.Trim,"mima",edtyhqpass.Text.Trim),"")
	Else
		comm.TL("优惠券卡号密码不能为空")
	End If
End Sub

Sub btndlgcancel_Click
	bd.CloseDialog(DialogResponse.CANCEL)
End Sub

Sub btnadd_Click
	Dim pbody As Panel
	Dim nh As Int=180dip
	pbody=comm.CreateCustomPanel(Activity,80%x,45%y)
	pbody.LoadLayout("dlgaddyhq")
	Dim tp As Panel=pbody.GetView(0)
	Dim dlgParams As BD_CustomDlgParams
	dlgParams.Initialize
	dlgParams.DialogBody = pbody
	dlgParams.BodyWidth = pbody.Width
	dlgParams.BodyHeight = tp.Height
	'dlgParams.Duration = 30 '自动关闭
	Dim DR As Int = bd.CustomDialog(dlgParams, "CD1")
	If DR==DialogResponse.POSITIVE Then
'		lstData.Add((lstData.Size+1)&"")
'		ulv.AddItem("itemyhq",lstData.Size)
		reqYhq
	End If
End Sub

Sub ivback_Click
	Activity.Finish	
End Sub